package hfuu.com.cn.teasystemapp;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import hfuu.com.cn.teasystemapp.bean.land;
import hfuu.com.cn.teasystemapp.bean.pick;
import hfuu.com.cn.teasystemapp.bean.user;
import hfuu.com.cn.teasystemapp.util.SerializableMap;
import hfuu.com.cn.teasystemapp.util.listviewutil;

public class pickactivity extends AppCompatActivity {
    List<pick> pickList;
    List<user> userList;
    List<land> landList;
    int sw = 0;
    View listview;
    private List<HashMap<String, Object>> pickdata;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pickactivity);
        Intent i = getIntent();
        pickList = new ArrayList<pick>();
        landList = new ArrayList<land>();
        userList = new ArrayList<user>();
        pickList = (List<pick>) i.getSerializableExtra("picklist");
        userList = (List<user>) i.getSerializableExtra("userlist");
        landList = (List<land>) i.getSerializableExtra("landlist");
        //pickdata.clear();
        pickdata = new ArrayList<HashMap<String, Object>>();
        for (pick pick :
                pickList) {
            HashMap<String, Object> item = new HashMap<String, Object>();
            item.put("pickid", pick.getPickId());
            item.put("teaname", pick.getPickTeaname());
            item.put("picktotal", pick.getPickQuality());
            for (land land:
                 landList) {
                if(land.getLandId().equals(pick.getPickLandid())) {
                    item.put("picklandname", land.getLandName());
                    item.put("picklandid",land.getLandId());
                }
            }
            item.put("type", pick.getPickOperate());
            if(pick.getPickOperate()==0){
                item.put("typename","折采");
            }else if(pick.getPickOperate()==1 ){
                item.put("typename","提手采");

            }else if(pick.getPickOperate()==2){
                item.put("typename","双手采");
            }else if(pick.getPickOperate()==3){
                item.put("typename","其他");
            }

            item.put("isfocus", false);
            for (user user :
                    userList) {
                Log.i("user",user.toString());
                if (pick.getPickUserid().equals(user.getUserId()))
                    item.put("pickuser", user.getUserName());
            }
            pickdata.add(item);
        }

        ImageView back = (ImageView) findViewById(R.id.pickback);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        ListView picklistview = (ListView) findViewById(R.id.picklist);
        final SimpleAdapter adapter = new SimpleAdapter(this, pickdata, R.layout.picklistitem, new String[]{"teaname", "picktotal", "pickuser"}, new int[]{R.id.teaname, R.id.picktotal, R.id.pickuser}) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                Boolean isFocus = (Boolean) pickdata.get(position).get("isfocus");

                if (isFocus) {
                    view.setBackgroundColor(getResources().getColor(R.color.tv_gray));
                } else {
                    view.setBackgroundColor(Color.WHITE);
                }
                return view;
            }
        };

        picklistview.setAdapter(adapter);
        listviewutil.setListViewHeightBasedOnChildren(picklistview);
        picklistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                sw = 0;
                pickdata.get(position).put("isfocus", true);
                for (int i = 0; i < pickdata.size(); i++) {
                    if (i != position) {
                        pickdata.get(i).put("isfocus", false);
                        adapter.getView(i, view, parent);
                    }

                }
                adapter.getView(position, view, parent);
                adapter.notifyDataSetChanged();
                final SerializableMap serializableMap=new SerializableMap();
                Map m=new HashMap();
                m= pickdata.get(position);
                Log.i("pick",m.toString());
                serializableMap.setMap(m);


                AlertDialog.Builder dialog = new AlertDialog.Builder(pickactivity.this).setTitle("修改").setMessage("确认修改数据").setPositiveButton("是",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                               Bundle b=new Bundle();
                               b.putSerializable("map",serializableMap);
                                //修改
                                Intent i=new Intent();
                                i.setClass(pickactivity.this,pickedit.class);
                                i.putExtra("landlist",(Serializable) landList);
                                i.putExtras(b);
                                startActivity(i);
                            }
                        }).setNeutralButton("否", null);
                dialog.show();

            }
        });

        FloatingActionButton fb= (FloatingActionButton) findViewById(R.id.pickadd);
        fb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("landland",landList.toString());
                 Intent i=new Intent();
                i.setClass(pickactivity.this,pickaddactivity.class);
                i.putExtra("landlist",(Serializable) landList);
                startActivity(i);
            }
        });

    }
}
